<?php
namespace app\admin\controller;
class Apply extends Common
{
    public function guanlian()
    {
        if(input('client_id'))
        {
            $admin_id = session('uid','','admin');
            $clientId = input('client_id');
            $clientInfo = db('client')->find($clientId);
            $applyInfo = db('apply')->where('client_id',$clientId)->where('admin_id',$admin_id)->where('status',0)->find();
            if(request()->isAjax())
            {
                $data = input('post.')['data'];
                if(!$applyInfo)
                {
                    $data['admin_id'] = $admin_id;
                    $data['client_id'] = $clientId;
                    $data['add_time'] = time();
                    $doStatus = db('apply')->insert($data);
                }
                else
                {
                    $data['id'] = $applyInfo['id'];
                    $doStatus = db('apply')->update($data);
                }
                if($doStatus  )
                {
                    return ['msg'=>'操作成功','icon'=>1];
                }
                else
                {
                    return ['msg'=>'操作失败','icon'=>2];
                }
            }

            $this->assign([
                'clientInfo'=>$clientInfo,
                'applyInfo'=>$applyInfo,
            ]);
            return view();
        }
    }

    public function view()
    {
        if(input('id')){
            $applyInfo = db('apply')->find(input('id'));
            $clientId = $applyInfo['client_id'];
            $clientInfo = db('client')->find($clientId);

            $this->assign([
                'clientInfo'=>$clientInfo,
                'applyInfo'=>$applyInfo,
            ]);
            return view();
        }
    }
    
    public function reply()
    {
        if(request()->isAjax())
        {
            $data = input('post.')['data'];
            $data['reply_time'] = time();
            $data['reply_admin_id'] = session('uid','','admin');
            $doStatus = db('apply')->update($data);
            if($doStatus  )
            {
                if($data['status'] == 1)
                {
                    $applyInfo = db('apply')->find($data['id']);
                    $bao_A_qian_time = db('conf')->where('ename','bao_A_qian_time')->value('value');
                    $end_time = strtotime('+'.$bao_A_qian_time.' day');
                    db('client')->where('id',$applyInfo['client_id'])->update(['xaid'=>$applyInfo['admin_id'],'status'=>1,'end_time'=>$end_time]);
                }
                return ['msg'=>'操作成功','icon'=>1];
            }
            else
            {
                return ['msg'=>'操作失败','icon'=>2];
            }
        }
    }

    public function applyList(){
        $adminId = session('uid','','admin');
        $userInfo=db('admin')->where('id',$adminId)->find();
        $groupId = $userInfo['group_id'];

        $applyList = db('apply')
                    ->alias('a')
                    ->field('a.*,c.activity_time,c.name,c.activity,c.address')
                    ->join('client c','a.client_id=c.id');
        if(input('status'))
        {
            $applyList = $applyList->where('a.status',input('status'));
        }
        if($groupId == 5)
        {
            $groupMembers = $userInfo['group_members'];
            $groupMembers = $groupMembers?$groupMembers.','.$adminId:$adminId;
            $applyList=$applyList->where('a.admin_id','IN',$groupMembers);
        }
        else if($groupId != 1)
        {
            $applyList=$applyList->where('a.admin_id',$adminId);
        }
        $applyList = $applyList->order('a.id desc')->paginate(10);
        $this->assign([
            'applyList'=>$applyList,
        ]);
        return view();
    }
    
    function clientAdd()
    {
        $json = input()['dataJson'];
        if($json)
        {
            if(request()->isAjax())
            {
                $data = input('post.')['data'];
                $data['add_admin_id'] = session('uid','','admin');
                $data['status'] = 0;
                $data['client_data'] = $json;
                $data['add_time'] = time();
                $sqlStatus = db('client_apply')->insert($data);
                if($sqlStatus  )
                {
                    return ['msg'=>'操作成功','icon'=>1];
                }
                else
                {
                    return ['msg'=>'操作失败','icon'=>2];
                }
            }


            $clientInfo = json_decode($json,true);
            $clientInfo['phone'] = json_decode($clientInfo['phone'],true);
            $clientInfo['count'] = getCountByClientId($clientInfo['union_id']);
            $this->assign([
                'clientInfo'=>$clientInfo,
            ]);
            return view();
        }
    }
    
    function clientList()
    {
        $adminId = session('uid','','admin');
        $groupId = session('group_id','','admin');
        $list = db('client_apply');
        if(input('status'))
        {
            $list = $list->where('status',input('status'));
        }
        if($groupId == 5)
        {
            $groupMembers = implode(',', session('group_members','','admin')) ;
            $groupMembers = $groupMembers?$groupMembers.','.$adminId:$adminId;
            $list = $list->where('add_admin_id','IN',$groupMembers);
        }
        if($groupId == 2)
        {
            $list = $list->where('add_admin_id',$adminId);
        }
        $list = $list->order('id desc')->paginate(10);
        $this->assign([
            'list'=>$list,
        ]);
        return view();
    }
    
    
    function clientReply()
    {
        if(input('id')){
            $info = db('client_apply')->find(input('id'));
    
            if(request()->isAjax())
            {
                $data = input('post.')['data'];
                $data['id'] = input('id');
                $data['reply_time'] = time();
                $data['reply_admin_id'] = session('uid','','admin');
                $doStatus = db('client_apply')->update($data);
                if($doStatus)
                {
                    if($data['status'] == 1)
                    {
                        $clientData = json_decode($info['client_data'],true);
                        $clientData['is_apply'] = 1;
                        $clientData['add_time'] = time();
                        $clientData['end_time'] = strtotime('+'.db('conf')->where('ename','bao_A_qian_time')->value('value').' day');
                        db('client')->insert($clientData);
                    }
                    return ['msg'=>'操作成功','icon'=>1];
                }
                else
                {
                    return ['msg'=>'操作失败','icon'=>2];
                }  
            }

            $clientInfo = json_decode($info['client_data'],true);
            $clientInfo['phone'] = json_decode($clientInfo['phone'],true);
            $clientInfo['count'] = getCountByClientId($clientInfo['union_id']);
            $this->assign([
                'clientInfo'=>$clientInfo,
                'info'=>$info,
            ]);
            return view();
        }
    }
    
    public function contractList()
    {
        $adminId = session('uid','','admin');
        $groupId = session('group_id','','admin');
        $list = db('client')->where('chat_check_status',-1)->where('status',4);
        if($groupId == 5)
        {
            $groupMembers = implode(',', session('group_members','','admin')) ;
            $groupMembers = $groupMembers?$groupMembers.','.$adminId:$adminId;
            $list = $list->where('xaid','IN',$groupMembers);
        }
        if($groupId == 2)
        {
            $list = $list->where('xaid',$adminId);
        }
        $list = $list->order('id desc')->paginate(10);
        $this->assign([
            'list'=>$list,
        ]);
        return view();
    }
    
    public function contractCheck()
    {
        if(input('id'))
        {
            if(request()->isAjax())
            {
                $data = input('post.')['data'];
                $data['id'] = input('id');
                if($data['status'] == 1)
                {
                    db('client')->where('id',$data['id'])->update(['chat_check_status'=>1,'qiandan_time'=>time()]);
                }
                else if($data['status'] == -1)
                {
                    db('client')->where('id',$data['id'])->update(['chat_check_status'=>0,'status'=>3]);
                }
                return ['msg'=>'操作成功','icon'=>1];
            }
            else
            {
                $this->assign([
                    'clientInfo'=>db('client')->find(input('id'))
                ]);
                return view();
            }
            
        }
    }
}